package com.easylinkin.linkappapi.alarm.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.alarm.entity.Alarm;
import com.easylinkin.linkappapi.alarm.entity.AlarmAnalyzeStatistic;
import com.easylinkin.linkappapi.alarm.service.AlarmService;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.elasticsearch.entity.EsQuerymodel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Tongjie
 * @since 2020-04-25
 */
@RestController
@Api(value="告警控制器",tags={"告警控制器"})
@RequestMapping("/alarm/alarmInfo")
public class AlarmController {

    @Resource
    private AlarmService service;

    @PostMapping("getPage")
    @ApiOperation("查询告警分页列表")
    public RestMessage getAlarmPage(@RequestBody RequestModel<Alarm> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getCustomQueryParams().getDevice(), "参数设备不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<Alarm> record = service.getAlarmPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(record).build();
    }

    /**
     * 获取 20分钟以内的告警数据
     */
    @GetMapping("getRecentAlarms")
    @ApiOperation("获取20分钟以内的告警数据")
    public RestMessage getRecentAlarms() {
        List<Alarm> alarms = service.getRecentAlarms();
        return RestBuilders.successBuilder().data(alarms).build();
    }
//    @PostMapping("getPageGlobal")
//    @ApiOperation("查询告警分页列表-所有项目")
//    public RestMessage getAlarmPageGlobal(@RequestBody RequestModel<Alarm> requestModel) {
//        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
//        Assert.notNull(requestModel.getPage(), "page 不能为空");
//        IPage<Alarm> record = service.getAlarmPageGlobal(requestModel.getPage(), requestModel.getCustomQueryParams());
//        return RestBuilders.successBuilder().data(record).build();
//    }

    /**
     * 获取当前服务端口，5042
     * @return
     */
    @ApiOperation("获取当前服务端口")
    @PostMapping("getCurrentServicePort")
    public RestMessage getCurrentServicePort() {
        return RestBuilders.successBuilder().data(service.getCurrentServicePort()).build();
    }


    @PostMapping("exportData")
    @ApiOperation("告警工单导出")
    public void exportData(@RequestBody Alarm alarm, HttpServletRequest request, HttpServletResponse response) {
        service.exportData(alarm, request, response);
    }

    /**
     * 电力监控页告警日志查询
     */
    @ApiOperation("电力监控页告警日志查询")
    @PostMapping("getDistributionAlarmPage")
    public RestMessage getDistributionAlarmPage(@RequestBody RequestModel<Alarm> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<Alarm> record = service.getDistributionAlarmPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(record).build();
    }

    /**
     * 根据资产ID获取资产关联设备的告警信息
     * @param requestModel
     * @return
     */
    @ApiOperation("根据资产ID获取资产关联设备的告警信息")
    @PostMapping("getAlarmPageByAsset")
    public RestMessage getAlarmPageByAsset(@RequestBody RequestModel<Alarm> requestModel) {
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<Alarm> record = service.getAlarmPageByAsset(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(record).build();
    }

    /**
     * 根据告警主键ID查询告警详细信息, 关联的全部设备（无 delete_state == 1 查询条件）
     * @param alarm
     * @return
     */
    @ApiOperation("根据告警主键ID查询告警详细信息")
    @PostMapping("getDetail")
    public RestMessage getDetail(@RequestBody Alarm alarm) {
        Assert.notNull(alarm, "alarm 不能为空");
        Assert.notNull(alarm.getId(), "id 不能为空");
        return RestBuilders.successBuilder().data(service.getDetail(alarm)).build();
    }

    /**
     * 根据告警主键ID查询告警详细信息, 存在设备的告警信息（delete_state == 1）
     * @param alarm
     * @return
     */
    @ApiOperation("根据告警主键ID查询告警详细信息（排除删除设备）")
    @PostMapping("getDetail2")
    public RestMessage getDetail2(@RequestBody Alarm alarm) {
        Assert.notNull(alarm, "alarm 不能为空");
        Assert.notNull(alarm.getId(), "id 不能为空");
        return RestBuilders.successBuilder().data(service.getDetail2(alarm)).build();
    }


    @ApiOperation("查询设备原报文")
    @PostMapping("getSourceJson")
    public RestMessage getSourceJson(@RequestBody Alarm alarm) {
        Assert.notNull(alarm, "alarm 不能为空");
        Assert.notNull(alarm.getId(), "id 不能为空");
        return RestBuilders.successBuilder().data(service.getSourceJson(alarm)).build();
    }

    @ApiOperation("根据规则引擎名称统计告警数量")
    @PostMapping("getAlarmStatisticsByRule")
    public RestMessage getAlarmStatisticsByRule(@RequestBody Alarm alarm) {
        Assert.notNull(alarm, "alarm 不能为空");
        return RestBuilders.successBuilder().data(service.getAlarmStatisticsByRule(alarm)).build();
    }

    @ApiOperation("按截止时间计算告警处理统计数量")
    @PostMapping("getAlarmStatisticsByStatus")
    public RestMessage getAlarmStatisticsByStatus(@RequestBody Alarm alarm) {
        Assert.notNull(alarm, "alarm 不能为空");
        return RestBuilders.successBuilder().data(service.getAlarmStatisticsByStatus(alarm)).build();
    }

    /**
     * （首页）告警数据统计保存或更新
     * @param alarm
     * @return
     */
    @ApiOperation("（首页）告警数据统计保存或更新")
    @PostMapping("/getAlarmStatisticsAuditTask")
    public RestMessage getAlarmStatisticsAuditTask(@RequestBody Alarm alarm) {
    	service.getAlarmStatisticsAuditTask(alarm);
    	return RestBuilders.successBuilder().build();
    }

    /**
     * 告警数量统计
     *
     * @param alarm
     * @return
     */
    @ApiOperation("告警数量统计")
    @PostMapping("/countAlarm")
    public RestMessage countAlarm(@RequestBody Alarm alarm) {
        int count = service.countAlarm(alarm);
        return RestBuilders.successBuilder().data(count).build();
    }

    @ApiOperation("顺丰大屏使用,根据空间统计下一级区域下的告警数量")
    @PostMapping("/statisticAlarmCountBySpace")
    public RestMessage statisticAlarmCountBySpace(@RequestBody EsQuerymodel esQuerymodel) {
        AlarmAnalyzeStatistic aas = service.statisticAlarmCountBySpace(esQuerymodel);
        return RestBuilders.successBuilder().data(aas).build();
    }

    @ApiOperation("顺丰大屏:环境监测预警分析")
    @PostMapping("/statisticNormalUserAlarmCount")
    public RestMessage statisticNormalUserAlarmCount(@RequestBody EsQuerymodel esQuerymodel) {
        AlarmAnalyzeStatistic aas = service.statisticNormalUserAlarmCount(esQuerymodel);
        return RestBuilders.successBuilder().data(aas).build();
    }

    @ApiOperation("数字工地大屏查询告警信息")
    @GetMapping("/getAlarmListForBigScreen")
    public RestMessage getAlarmListForBigScreen() {
        return RestBuilders.successBuilder().data(service.getAlarmListForBigScreen()).build();
    }

    @ApiOperation("安全管理大屏查询告警信息")
    @GetMapping("security")
    public RestMessage countSecurityAlarmList() {
        Map<String, Object> map = service.countSecurityAlarmList();
        return RestBuilders.successBuilder().data(map).build();
    }

    @ApiOperation("安全管理大屏查询告警信息列表")
    @PostMapping("security/list")
    public RestMessage getSecurityAlarmList(@RequestBody RequestModel<Alarm> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<Alarm> record = service.getSecurityAlarmList(requestModel);
        return RestBuilders.successBuilder().data(record).build();
    }

}
